
package com.aizhixin.lab.task.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aizhixin.lab.task.domain.*;
import com.aizhixin.lab.task.entity.*;
import com.aizhixin.lab.task.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aizhixin.lab.account.dto.AccountDTO;
import com.aizhixin.lab.account.service.AccountService;
import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.TokenUtil;
import com.aizhixin.lab.course.course.entity.Course;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("/api/v1/task/teacher")
@Api(value = "发布任务操作API", description = "发布任务操作API")
public class TeacherTaskController {
    @Autowired
    private AccountService accountService;
    @Autowired
    private TaskStuInforService taskStuInforService;
    @Autowired
    private ReleaseTaskService releaseTaskService;
    @Autowired
	private TaskService taskService;

    @RequestMapping(value = "/gettaskstulist", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(httpMethod = "GET", value = "发布任务的学生列表", response = Void.class, notes = "发布任务的学生列表<br>@author zhengning")
    public ResponseEntity<?> getCourseClassList(@RequestHeader("Authorization") String token,
                                                @ApiParam(value = "releaseTaskId 发布任务id", required = true) @RequestParam(value = "releaseTaskId", required = true) String releaseTaskId,
                                                @ApiParam(value = "taskStatus 学生任务状态uncommit:未提交,pending:待审批,approved:已审批,不填查所有", required = false) @RequestParam(value = "taskStatus", required = false) String taskStatus) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
        }
        Map<String, Object> result = new HashMap();
        List<TaskStuInfor> list = null;
        if (StringUtils.isEmpty(taskStatus)) {
            list = taskStuInforService.findAllByReleaseTaskId(releaseTaskId);
        } else {
            list = taskStuInforService.findAllByReleaseTaskIdAndTaskStatus(releaseTaskId, taskStatus);
        }
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.DATA, list);
        return new ResponseEntity(result, HttpStatus.OK);
    }

    @RequestMapping(value = "/findCodeEvaluating", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询代码综合评分", httpMethod = "GET", notes = "查询代码综合评分")
    public ResponseEntity<?> findCodeEvaluating(@RequestHeader("Authorization") String token,
                                                @ApiParam(value = "发布任务题目id", required = true) @RequestParam String releaseQuestionId,
                                                @ApiParam(value = "学生Id", required = true) @RequestParam String stuId) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        StuTaskCodeEvaluatingDomain stuTask = taskStuInforService.findCodeEvaluating(releaseQuestionId,stuId);
        return new ResponseEntity<>(stuTask, HttpStatus.OK);
    }

    @RequestMapping(value = "/findReferenceCode", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "学生提交的代码和老师参考代码", httpMethod = "GET", notes = "学生提交的代码和老师参考代码")
    public ResponseEntity<?> findReferenceCode(@RequestHeader("Authorization") String token,
                                               @ApiParam(value = "发布任务题目id", required = true) @RequestParam String releaseQuestionId,
                                               @ApiParam(value = "学生Id", required = true) @RequestParam String stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        StuTaskReferenceCodeDomain stuTask = taskStuInforService.findReferenceCode(releaseQuestionId,stuId);
        return new ResponseEntity<>(stuTask, HttpStatus.OK);
    }

    @RequestMapping(value = "/findEvaluationData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询评测统计表", httpMethod = "GET", notes = "查询评测统计表,代码优化建议也在其中")
    public ResponseEntity<?> findEvaluationData(@RequestHeader("Authorization") String token,
                                               @ApiParam(value = "发布任务题目id", required = true) @RequestParam String releaseQuestionId,
                                               @ApiParam(value = "学生Id", required = true) @RequestParam String stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        TaskStuStatistics stuTask = taskStuInforService.findEvaluationData(releaseQuestionId,stuId);
        return new ResponseEntity<>(stuTask, HttpStatus.OK);
    }

    @RequestMapping(value = "/findChartData", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "历次提交代码折线统计图数据", httpMethod = "GET", notes = "历次提交代码折线统计图数据")
    public ResponseEntity<?> findEchartData(@RequestHeader("Authorization") String token,
                                               @ApiParam(value = "发布任务题目id", required = true) @RequestParam String releaseQuestionId,
                                               @ApiParam(value = "学生Id", required = true) @RequestParam String stuId) {
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        Integer[] scoreArray = taskStuInforService.findChartData(releaseQuestionId, stuId);
        return new ResponseEntity<>(scoreArray, HttpStatus.OK);
    }

    @RequestMapping(value = "/findProgrammeAndRubric", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "查询编程题目和其他编程题", httpMethod = "GET", notes = "查询编程题目和其他编程题")
    public ResponseEntity<?> findProgrammeAndRubric(@RequestHeader("Authorization") String token,
                                                @ApiParam(value = "发布任务题目id", required = true) @RequestParam String releaseQuestionId,
                                                @ApiParam(value = "任务id", required = true) @RequestParam String releaseTaskId) {
        Map<String, Object> result = new HashMap<String, Object>();
        AccountDTO account = accountService.getUserByToken(token);
        if (account == null) {
            return new ResponseEntity<Object>(TokenUtil.tokenValid(),
                    HttpStatus.UNAUTHORIZED);
        }
        FindTaskTopicDomain stuTask = taskStuInforService.findProgrammeAndRubric(releaseQuestionId,releaseTaskId);
        return new ResponseEntity<>(stuTask, HttpStatus.OK);
    }


//    @RequestMapping(value = "/getTaskList", method = RequestMethod.POST)
//	@ApiOperation(httpMethod = "POST", value = "获取任务列表", response = Void.class, notes = "获取任务列表<br>@author lwq")
//    public ResponseEntity<?> getTaskList(@RequestHeader("Authorization") String token,
//                                         @ApiParam(value = "QueryDomain ", required = false) @RequestBody QueryDomain domain ){
//
//		AccountDTO account = accountService.getUserByToken(token);
//		if (account == null) {
//			return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//		}
//		Map<String, Object> taskList = taskService.getTaskList(domain, account);
//		return new ResponseEntity<>(taskList, HttpStatus.OK);
//    }


//	@RequestMapping(value = "/approvetask", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
//	@ApiOperation(value = "批阅学生任务", httpMethod = "PUT", notes = "批阅学生作业<br><br><b>xiagen</b>")
//	public ResponseEntity<?> pyWorkStuInfo(@RequestHeader("Authorization") String token,
//			@ApiParam(value = "<b>必填</b><br>stuTaskId:学生任务的id<br>id:学生答案id的列表<br><b>选填</b><br>result:批阅结果<br>comment:评语list列表<br>") @RequestBody ApproveDomain pyDomain) {
//		Map<String, Object> resultResponse = new HashMap<String, Object>();
//		AccountDTO account = accountService.getUserByToken(token);
//		if (account == null) {
//			return new ResponseEntity<Object>(TokenUtil.tokenValid(), HttpStatus.UNAUTHORIZED);
//		}
//		if (pyDomain.getStuWorkId() == null || pyDomain.getStuWorkId().equals("")) {
//			resultResponse.put(ApiReturnConstants.RESULT, Boolean.FALSE);
//			resultResponse.put(ApiReturnConstants.CAUSE, "id为空");
//			return new ResponseEntity<Map<String, Object>>(resultResponse, HttpStatus.EXPECTATION_FAILED);
//		}
//		WorkStuInfo workStuInfo = null;
//
//		try {
//			workStuInfo = workStuInfoService.findById(pyDomain.getStuWorkId());
//			// System.out.println(workStuInfo.getWorkRelease().getId());
//			if (workStuInfo == null) {
//				resultResponse.put(ApiReturnConstants.RESULT, Boolean.FALSE);
//				resultResponse.put(ApiReturnConstants.CAUSE, "此学生没有作业信息");
//				return new ResponseEntity<Map<String, Object>>(resultResponse, HttpStatus.EXPECTATION_FAILED);
//			}
//
//		} catch (Exception e) {
//			resultResponse.put(ApiReturnConstants.RESULT, Boolean.FALSE);
//			resultResponse.put(ApiReturnConstants.CAUSE, e);
//			resultResponse.put(ApiReturnConstants.CODE, PublicErrorCode.QUERY_EXCEPTION.getIntValue());
//			return new ResponseEntity<Map<String, Object>>(resultResponse, HttpStatus.EXPECTATION_FAILED);
//		}
//		if (pyDomain.getComment().isEmpty()) {
//			workStuInfo = workStuInfoService.updateworkStuInfo(pyDomain.getStuWorkId(), pyDomain.getResult(), null,
//					pyDomain.getId(),false,dto.getName());
//		} else {
//
//			workStuInfo = workStuInfoService.updateworkStuInfo(pyDomain.getStuWorkId(), pyDomain.getResult(),
//					pyDomain.getComment(), pyDomain.getId(),false,dto.getName());
//		}
//		StuWorkIdAndBatchTimeDateDomain StuWorkIdAndBatchTimeDate=new StuWorkIdAndBatchTimeDateDomain();
//		StuWorkIdAndBatchTimeDate.setBatchTime(workStuInfo.getBatchTime());
//		StuWorkIdAndBatchTimeDate.setWorkStuId(workStuInfo.getId());
//		StuWorkIdAndBatchTimeDate.setResult(workStuInfo.getResult());
//		resultResponse.put(ApiReturnConstants.RESULT, Boolean.TRUE);
//		resultResponse.put(ApiReturnConstants.DATA, StuWorkIdAndBatchTimeDate);
//		return new ResponseEntity<Map<String, Object>>(resultResponse, HttpStatus.OK);
//	}
}
